package DAO;

import core.BlogException;
import core.Clanek;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author vojta
 * DAO trida pro Clanek
 */
public class ClanekDAO {

    Connection cn;

    public ClanekDAO(Connection cn) {
        this.cn = cn;
    }

    /**
     * Prida novy clanek do databaze
     * @param c - Clanek
     * @throws BlogException 
     */
    public void addClanek(Clanek c) throws BlogException {
        try {
            //---------Zalozime clanek------------------
            PreparedStatement stmt = cn.prepareStatement("INSERT INTO Clanky(text,kategorie, nazev,cas) VALUES(?,?,?,NOW())");
            stmt.setString(1, c.getText());
            stmt.setInt(2, c.getKategorie());
            stmt.setString(3, c.getNazev());

            stmt.executeUpdate();

            System.out.println(stmt);

        } catch (SQLException ex) {
            throw new BlogException("Chyba pri vytvareni Uzivatele!");
        }
    }

    /**
     * Odstrani clanek z databaze
     * @param c - CLanek
     * @throws BlogException 
     */
    public void removeClanek(Clanek c) throws BlogException {
        try {
            PreparedStatement stmt = cn.prepareStatement("DELETE FROM Clanky WHERE id=?");
            stmt.setInt(1, c.getId());
            stmt.executeUpdate();
        } catch (SQLException ex) {
            throw new BlogException("Chyba pri odstranovani clanku");
        }
    }

    /**
     * Zedituje clanek podle jeho id
     * @param c
     * @throws BlogException 
     */
    public void editClanek(Clanek c) throws BlogException {
        try {
            PreparedStatement stmt = cn.prepareStatement("UPDATE Clanky SET nazev=?, text=?, kategorie=?  WHERE id=?");
            stmt.setString(1, c.getNazev());
            stmt.setString(2, c.getText());            
            stmt.setInt(3, c.getKategorie());            
            stmt.setInt(4, c.getId());

            stmt.executeUpdate();

        } catch (SQLException ex) {
            throw new BlogException("Chyba pri editaci clanku!");
        }
    }

    /**
     * Ziska z databaze clanek podle jeho ID
     * @param c - Clanek
     * @return Clanek
     * @throws BlogException 
     */
    public Clanek getClanek(Clanek c) throws BlogException {
        try {
            PreparedStatement stmt = cn.prepareStatement("SELECT * FROM Clanky WHERE id=?");
            stmt.setInt(1, c.getId());
            ResultSet rs = stmt.executeQuery();

            Clanek cl = new Clanek();
            rs.next();
            cl.setId(rs.getInt("id"));            
            
            //ziskani jednotlivych property
            cl.setKategorie(rs.getInt("kategorie"));
            cl.setNavstevnost(rs.getInt("navstevnost"));
            cl.setNazev(rs.getString("nazev"));
            cl.setText(rs.getString("text"));
            cl.setLibi(rs.getInt("libi"));
            cl.setNelibi(rs.getInt("nelibi"));
            
            return cl;

        } catch (SQLException ex) {

            throw new BlogException("Chyba pri ziskavani clanku!");
        }
    }

    /**
     * Ziska vsechny clanky v dane kategorii
     * @param kat - cislo kategorie
     * @return List<Clanek> - seznam clanku
     * @throws BlogException 
     */
    public List<Clanek> getClankyVKategorii(int kat) throws BlogException {
        try {
            PreparedStatement stmt = cn.prepareStatement("SELECT * FROM Clanky WHERE kategorie=?");
            stmt.setInt(1, kat);
            ResultSet rs = stmt.executeQuery();

            List<Clanek> list = new ArrayList<Clanek>();

            //projdeme cely resultSet a naskladame ziskane clanky do Listu
            while (rs.next()) {                
                Clanek cl = new Clanek();
                cl.setId(rs.getInt("id"));
                
                list.add(this.getClanek(cl));
            }

            return list;

        } catch (SQLException ex) {
            throw new BlogException("Chyba pri ziskavani clanku!");
        }
    }
    
    /**
     * Zvysis navstevnost daneho clanku o 1
     * @param c - Clanek
     * @throws BlogException 
     */
    public void pridejPrecteniClanku(Clanek c) throws BlogException {
        try {
            PreparedStatement stmt = cn.prepareStatement("UPDATE Clanky SET navstevnost=navstevnost+1 WHERE id=?");
            stmt.setInt(1, c.getId());
            
            stmt.executeUpdate();
            
        } catch (SQLException ex) {
            throw new BlogException("Chyba pri zvysovani navstevnosti!");
        }
    }
    
    /**
     * Zvysi hodnoceni clanku - tj. prida Libi+1
     * @param c
     * @throws BlogException 
     */
    public void zvysHodnoceniClanku(Clanek c) throws BlogException {
         try {
            PreparedStatement stmt = cn.prepareStatement("UPDATE Clanky SET libi=libi+1 WHERE id=?");
            stmt.setInt(1, c.getId());
            
            stmt.executeUpdate();
            
        } catch (SQLException ex) {
            ex.printStackTrace();
            throw new BlogException("Chyba pri zvysovani hodnoceni!");
        }
    }
     
    /**
     * Snizi hodnoceni clanku - tj. udela Nelibi+1
     * @param c
     * @throws BlogException 
     */
    public void snizHodnoceniClanku(Clanek c) throws BlogException {
         try {
            PreparedStatement stmt = cn.prepareStatement("UPDATE Clanky SET nelibi=nelibi+1 WHERE id=?");
            stmt.setInt(1, c.getId());
            
            stmt.executeUpdate();
            
        } catch (SQLException ex) {            
            throw new BlogException("Chyba pri snizovani hodnoceni!");
        }
    }
    
    /**
     * Ziska seznam x nejnovejsich clanku
     * @param pocet - pocet clanku, ktere ma vratit
     * @return List<Clanek> - seznam clanku
     * @throws BlogException 
     */
    public List<Clanek> getNejnovejsiClanky(int pocet) throws BlogException {
        try {
            List<Clanek> list = new ArrayList<Clanek>();
            PreparedStatement stmt = cn.prepareStatement("SELECT * FROM Clanky ORDER BY cas LIMIT ?");
            stmt.setInt(1, pocet);
            ResultSet rs = stmt.executeQuery();
            
            while(rs.next()) {
                Clanek c = new Clanek();
                c.setId(rs.getInt("id"));
                list.add(this.getClanek(c));
            }
            
            return list;
            
        } catch (SQLException ex) {
            throw new BlogException("Chyba pri ziskavani clanku podle casu!");
        }
    }
    
    /**
     * Fulltextove prohleda vsechny clanky a ty se shodou vrati
     * @param text - hledany text
     * @return List<Clanek> - seznam clanku
     * @throws BlogException 
     */
    public List<Clanek> getFulltextClanky(String text) throws BlogException {
        try {
            List<Clanek> list = new ArrayList<Clanek>();
            PreparedStatement stmt = cn.prepareStatement("SELECT * FROM Clanky WHERE text LIKE '%"+ text +"%'");            
            ResultSet rs = stmt.executeQuery();
            
            while(rs.next()) {
                Clanek c = new Clanek();
                c.setId(rs.getInt("id"));
                list.add(this.getClanek(c));
            }
            
            return list;
            
        } catch (SQLException ex) {
            throw new BlogException("Chyba pri ziskavani clanku podle casu!");
        }
    }
}
